<?php
namespace app\index\controller;
use think\Db;
class Index
{
    public function index()
    {
       
    }

    // 请求登录连接
    public function login(){
        // var_dump(666);die;
        $username = input('username') ? input('username') : '';
        $gamename = input('gamename') ? input('gamename') : '';
        $score = input('score') ? input('score') : '0';
        $sign = input('sign') ? input('sign') : '';

        if($username==''){
            return json(['code'=>1020,'msg'=>'username不能为空']);
        }
        if($gamename==''){
            return json(['code'=>1020,'msg'=>'gamename不能为空']);
        }
        if($sign==''){
            return json(['code'=>1020,'msg'=>'sign不能为空']);
        }
        $data=[
            'username'=>$username,
            'gamename'=>$gamename,
            'score'=>$score,
        ];
        $checksign =  checksign($data,$sign);
            // if(!$checksign){
            //     return json(['code'=>1025,'msg'=>'签名错误']);
            // }
        $user = Db::name('users')->where('username',$username)->find();

        if(!$user){
            // 账号不存在 则自动创建账号
            $create = createuser($username,$score);
            $user['id']= $create['id'];
            $user['token']=$create['token'];
        }else{
            Db::name("users")->where('username',$username)->update(['score'=>$score]);
        }
        $url =config("dtasite").'uid='.$user['id'].'&token='.$user['token'].'&gamename='.$gamename;

        return json(['code'=>1,'msg'=>'成功','data'=>$url]);
    }

    // 创建账号
    public function createuser(){
   
        $username = input('username') ? input('username') : '';
        $score = input('score') ? input('score') : '';
        $gamename = input('gamename') ? input('gamename') : '';
        $sign = input('sign') ? input('sign') : '';

        if($username==''){
            return json(['code'=>1020,'msg'=>'username不能为空']);
        }
        if($score==''){
            return json(['code'=>1020,'msg'=>'score不能为空']);
        }
        if($sign==''){
            return json(['code'=>1020,'msg'=>'sign不能为空']);
        }
        $data=[
            'username'=>$username,
            'gamename'=>$gamename,
            'score'=>$score,
        ];
       $checksign =  checksign($data,$sign);
        if(!$checksign){
            return json(['code'=>1025,'msg'=>'签名错误']);
        }

       $create = createuser($username,$score);
       if($create==1031){
            return json(['code'=>1031,'msg'=>'用户ID已经存在']);
       }
       if($create==1038){
        return json(['code'=>1032,'msg'=>'用户名已经存在']);
       }
       if($create==1032){
        return json(['code'=>1032,'msg'=>'用户创建失败']);
       }
   
       return json(['code'=>1,'msg'=>'用户创建成功','data'=>$create]);

    }
    // 签名sign
    public function make(){
        $uid = input('uid') ? input('uid') : '';
        $username = input('username') ? input('username') : '';
        $score = input('score') ? input('score') : '';
        $gamename = input('gamename') ? input('gamename') : '';
        $type = input('type') ? input('type') : '';
        $data=[
            'username'=>$username,
            'score'=>$score,
            'type'=>$type,
        ];
       $checksign =  makesign($data);
       var_dump($checksign);die;
       
    }
    public function makee() {
            $uid = input('uid') ? input('uid') : '';
            $username = input('username') ? input('username') : '';
            $score = input('score') ? input('score') : '';
            $type = input('type') ? input('type') : '';
            $data = [
                'username' => $username,
                'type'=>(int)$type,
                'score' => $score
            ];
            $checksign = makesign($data);
            return json(['code' => 1, 'msg' => '成功', 'data' => $checksign]);
        }
           
 
    public function getbalance(){
        $username = input('username') ? input('username') : '';
        $data=[
            'username'=>$username
        ];
        $sign = input('sign') ? input('sign') : '';
        $checksign =  checksign($data,$sign);
            if(!$checksign){
                return json(['code'=>1025,'msg'=>'签名错误']);
            }
        $user = Db::name('users')->where('username',$username)->find();
        if(!$user){
            return json(['code'=>1035,'msg'=>'用户不存在']);
        }

        return json(['code'=>1,'msg'=>'查询成功','data'=>['score'=>$user['score']]]);

    }

    public function addbalance(){
        $username = input('username') ? input('username') : '';
        $type = input('type') ? input('type') : '';
        $score = input('score') ? input('score') : '';
        $data=[
            'username'=>$username,
            'score'=>$score,
            'type'=>$type,
        ];
        // var_dump($data);die;
        $sign = input('sign') ? input('sign') : '';
        $checksign =  checksign($data,$sign);
            if(!$checksign){
                return json(['code'=>1025,'msg'=>'签名错误']);
            }
        $user = Db::name('users')->where('username',$username)->find();
        if(!$user){
            return json(['code'=>1035,'msg'=>'用户不存在']);
        }
        if($type==1){
            $do = Db::name('users')->where('username',$username)->setInc('score',$score);
        }else{
            $do =  Db::name('users')->where('username',$username)->setDec('score',$score);
        }
        $userinfo = getuserinfo2($username);

        return json(['code'=>1,'msg'=>'操作成功','data'=>['score'=>$userinfo['score']]]);
        

    }

        // 检查url
        public  function checkurl(){
            $data="uid=222&token=748484848484&coin=45151";
            $datas = explode('&',$data);
            foreach($datas as $k =>$v){
                $chai = explode('=',$v);
                
                $a[0]=$chai[0];
                $a[1]=$chai[1];
                $new[]=$a;
            }
            // var_dump($new);die;
            foreach($new as $kt =>$vt){
                $b[$vt[0]]=$vt[1];
                $new2=$b;
            }
            var_dump($new2['token']);
        }
    
    // 获取各种币的余额
    public function getUserInfoByMobile(){
        $username =input('username') ? input('username') : '';
        $data=[
            'username'=>$username,
        ];
        $sign =  makesign22($data);
        
        $url='https://test10.ygtimes.com/prod-api/app/user/getUserInfoByMobile?username='.$username.'&sign='.$sign;
        // var_dump($url);die;
        $res = file_get_contents($url);
     
        return $res;

    }

    // 操作余额
    public function setWalletValue(){
        $data['username']=input('username') ? input('username') : '';
        $data['name']=input('bizhong') ? input('bizhong') : ''; //币种名称
        $data['amount']=input('amount') ? input('amount') : '';; // 操作金额
        $data['type']=input('type') ? input('type') : '';; // 1 增加 2 减少
        $data['sign']=makesign22($data);
        $url = "https://test10.ygtimes.com/prod-api/app/wallet/setWalletValue";
        $data1 = json_encode($data);
        $res = sendpost($url,$data1);
        return $res;
    }


    // 定时任务 
    public function fenrun(){
        $option  = $this->getconfig();
        //昨天日奖池可分配
        $start_time=date('Y-m-d 00:00:00',strtotime('yesterday'));
        $end_time=date('Y-m-d 23:59:59',strtotime('yesterday'));
        // 今日
        // $start_time = strtotime(date('Y-m-d').'00:00:00');
        // $end_time= strtotime(date('Y-m-d').'23:59:59');

        $xiaohui2 =  Db::name("jiangchi")->where("addtime","between",[$start_time,$end_time])->where("roomid",2)->sum('xiaohui');
        $xiaohui3 =  Db::name("jiangchi")->where("addtime","between",[$start_time,$end_time])->where("roomid",3)->sum('xiaohui');
        $xiaohui4 =  Db::name("jiangchi")->where("addtime","between",[$start_time,$end_time])->where("roomid",4)->sum('xiaohui');
        // 人员列表
        $gudong = Db::name("xiaohui")->select();

        foreach($gudong as $k =>$v){
            
            $money2 = $v['rate']*$xiaohui2;
            $money3 = $v['rate']*$xiaohui3;
            $money4 = $v['rate']*$xiaohui4;

            $url2 = $option['jiekouurl'].'setWalletValue?username='.$v['account'].'&bizhong=eso&type=1&amount='.$money2;
            file_get_contents($url2);
            $url3 = $option['jiekouurl'].'setWalletValue?username='.$v['account'].'&bizhong=cn&type=1&amount='.$money3;
            file_get_contents($url3);
            $url4 = $option['jiekouurl'].'setWalletValue?username='.$v['account'].'&bizhong=es&type=1&amount='.$money3;
            file_get_contents($url4);

        }
        
    }

    public function getconfig(){
        $option = Db::name("options")->where("id",1)->find();
        $res = json_decode($option['value'],true);
        return $res['logo'];
    }



    // 游戏列表
    public function gamelist()
    {
        $list = Db::name('gamelist')->select();
        foreach($list as $k=>$v){
             $v['icon']=handleimage($v['icon']);
             $new[]=$v;
        }
        $banner = Db::name("banner")->select();
        foreach($banner as $kt=>$vt){
         $vt['image']=handleimage($vt['image']);
         $new2[]=$vt;
        }
        
        $info['banner']=$new2;
       
        $info['list']=$new;
        $res = ['code'=>1,'msg'=>'success','data'=>$info];
        echo(json_encode($res));exit();
    //    return $res;
    }

}
